import cloudscraper
from scrapy.downloadermiddlewares.retry import RetryMiddleware
from scrapy.http import HtmlResponse
from crawler.connection.tools import retry
from crawler.utils.func_tools import get_tunnel_ip


class CookiesMiddleware(RetryMiddleware):
    # ua_list = redis_conn.zget('user_agent_pool', count=-1, is_pop=False)

    @retry(retry_times=7)
    def set_headers(self, request):
        # count = redis_conn.incr('ua_count')
        # index = count % len(self.ua_list)
        # user_agent = self.ua_list[index]
        # print(user_agent)
        # headers = {
        #     'authority': 'www.ssense.com',
        #     'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
        #     'accept-language': 'zh-CN,zh;q=0.9',
        #     'cache-control': 'no-cache',
        #     'pragma': 'no-cache',
        #     'sec-ch-ua': '"Google Chrome";v="113", "Chromium";v="113", "Not-A.Brand";v="24"',
        #     'sec-ch-ua-mobile': '?0',
        #     'sec-ch-ua-platform': '"macOS"',
        #     'sec-fetch-dest': 'document',
        #     'sec-fetch-mode': 'navigate',
        #     'sec-fetch-site': 'none',
        #     'sec-fetch-user': '?1',
        #     'upgrade-insecure-requests': '1',
        #     'user-agent': user_agent,
        # }
        # headers = headers
        scraper = cloudscraper.create_scraper()
        response = scraper.get(url=request.url, proxies=get_tunnel_ip(), timeout=30)
        if 'Cloudflare' in response.text or 'Press & Hold to confirm you' in response.text:
            raise Exception('重试')

        response = HtmlResponse(request.url, body=response.text, encoding='utf-8', request=request)
        return response

    def process_request(self, request, spider):
        # print("request.url", request.url)
        response = self.set_headers(request)
        return response

    def process_response(self, request, response, spider):
        # print(response.text)
        if 'Press & Hold to confirm you' in response.text:
            return self._retry(request, 'Error Request, Robot Tests', spider)

        return response
